# coding: utf-8
import json
import time

from selenium import webdriver


class Douyu(object):
    def __init__(self):
        self.url = 'https://www.douyu.com/directory/all'
        self.driver = webdriver.Chrome()
        self.file = open('ext/E3_douyu.json', mode='w', encoding='utf-8')

    def parse_data(self):
        # 获取房间节点列表
        _node_list = self.driver.find_elements_by_xpath('//*[@id="live-list-contentbox"]/li/a')
        print(len(_node_list))
        _data_list = []
        for _node in _node_list:
            _temp = {}
            _temp['title'] = _node.find_element_by_xpath('./div/div/h3').text
            _temp['type'] = _node.find_element_by_xpath('./div/div/span').text
            _temp['owner'] = _node.find_element_by_xpath('./div/p/span[1]').text
            _temp['views'] = _node.find_element_by_xpath('./div/p/span[2]').text
            _temp['cover'] = _node.find_element_by_xpath('./span/img').get_attribute('data-original')
            _temp['url'] = _node.get_attribute('href')
            _data_list.append(_temp)
        return _data_list

    def save_data(self, data_list):
        for _data in data_list:
            _str_data = json.dumps(_data, ensure_ascii=False) + ',\n'
            self.file.write(_str_data)

    def __del__(self):
        self.driver.close()
        self.file.close()

    def run(self):
        self.driver.get(self.url)
        self.driver.maximize_window()
        time.sleep(3)
        while True:
            _data_list = self.parse_data()
            self.save_data(_data_list)
            try:
                _el_next = self.driver.find_element_by_xpath('//a[@class="shark-pager-next"]')
                _el_next.click()
                time.sleep(3)
                break
            except:
                break


def main():
    douyu = Douyu()
    douyu.run()


if __name__ == '__main__':
    main()
